package onehour.kafka.example.dhy;

import onehour.kafka.example.serialization.AvroDeserializer;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.consumer.OffsetAndMetadata;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.header.Header;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.util.*;

public class DhyConsumer {
    public static void main(String[] args) {
        Properties props = new Properties();
        props.setProperty("bootstrap.servers", "8.134.144.48:9093");
        props.setProperty("group.id", "group-1");
        //下面两个属性用于设置自动提交
        props.setProperty("enable.auto.commit", "false");
        props.setProperty("key.deserializer", StringDeserializer.class.getName());
        props.setProperty("value.deserializer", StringDeserializer.class.getName());

        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
        consumer.subscribe(Collections.singletonList("my-topic"));


        //指定消费分区
        String topic="foo";
        TopicPartition partition0 = new TopicPartition(topic, 0);
        TopicPartition partition1 = new TopicPartition(topic, 1);
        consumer.assign(Arrays.asList(partition0,partition1));

        //指定消费位置
        consumer.seek(partition0,1);

        final int minBatchSize = 200;
        while (true) {
            //一次性请求尽可能多的数据
            ConsumerRecords<String, String> records = consumer.poll(Long.MAX_VALUE);
            //获取所有分区
            for (TopicPartition partition : records.partitions()) {
                //依次处理每个分区下的记录
                List<ConsumerRecord<String, String>> partitionRecords = records.records(partition);
                for (ConsumerRecord<String, String> record : partitionRecords) {
                    //打印当前消息在当前分区下的偏移量和消息值
                    System.out.println(record.offset()+": "+record.value());
                    //获取当前分区最后一条消息的偏移量
                    long lastOffset=partitionRecords.get(partitionRecords.size()-1).offset();
                    //
                    consumer.commitAsync(Collections.singletonMap(partition,new OffsetAndMetadata(lastOffset+1)),null);
                }
            }
        }
    }
}
